package com.ebupt.migu.music.scenelabels.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.TokenUtils;
import com.ebupt.migu.music.label.mapper.IndustryLabelTableMapper;
import com.ebupt.migu.music.scenelabels.service.SceneLabelsService;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.mapper.UserMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2024/6/25 9:54
 */
@Slf4j
@Service
public class SceneLabelsServiceImpl implements SceneLabelsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IndustryLabelTableMapper industryLabelTableMapper;

    /**
     * 生成token
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject encrypt(String params) {
        ResultObject res = new ResultObject();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        res.setData(TokenUtils.createJwtToken(params));
        return res;
    }

    /**
     * token解密
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject decrypt(String params) {
        String token = String.valueOf(JSONObject.parseObject(params).get("token"));
        ResultObject res = new ResultObject();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        res.setData(TokenUtils.parseJWT(token).getId());
        return res;
    }

    /**
     * 回调接口
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject callback(String params) {
        ResultObject res = new ResultObject();
        try {
            log.info("scene labels params: {}", params);
            JSONObject entity = JSONObject.parseObject(params);
            String loginName = String.valueOf(entity.get("loginName"));
            Map<String, Object> login = new HashMap<>(3);
            login.put("pageStart", 0);
            login.put("rows", 9999);
            login.put("loginName", loginName);
            SysUser user = userMapper.queryByPage(login).get(0);
            HashMap<String, Object> customerInfo = new HashMap<>();
            // 添加登录用户身份信息到访问参数
            customerInfo.put("industryGroupId", String.valueOf(entity.get("customerId")));
            customerInfo.put("userGroupName", entity.get("customerName") + "_" + System.currentTimeMillis());
            customerInfo.put("creatorId", user.getLoginName());
            customerInfo.put("creatorName", user.getUserName());
            customerInfo.put("creatorDepartmentId", user.getDeptId());
            customerInfo.put("creatorDepartmentName", user.getDeptName());
            customerInfo.put("creatorPhone", user.getMobile());
            customerInfo.put("creatorEmail", user.getEmail());
            customerInfo.put("fileName", String.valueOf(entity.get("fileName")));
            customerInfo.put("decryptKey", String.valueOf(entity.get("decryptKey")));
            customerInfo.put("remark", String.valueOf(entity.get("remark")));
            customerInfo.put("mode", String.valueOf(entity.get("mode")));
            if ("2".equals(entity.get("mode"))) {
                customerInfo.put("status", "1");
            }
            log.info("拼装参数：{}", customerInfo);
            Integer integer = industryLabelTableMapper.queryByCustomerId(String.valueOf(entity.get("customerId")));
            if (null != integer && integer != 0) {
                industryLabelTableMapper.update(customerInfo);
            } else {
                industryLabelTableMapper.create(customerInfo);
            }
            return res.success();
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode("9999");
            res.setMsg("SYSTEM ERROR");
            return res;
        }
    }
}
